package com.twu.biblioteca;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Created by abhishep on 3/5/14.
 */
public class MenuClass  {

    Formatter formatter;
    final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

    public MenuClass(Formatter formatter) {
        this.formatter=formatter;
    }

    public void showMenu() {
        formatter.outputMessageToConsole("1.Book Library 2.Movie Library 3.User Information 4.Exit");
    }


    public void getCustomerID()
    {
        formatter.outputMessageToConsole("UserId :");
    }

    public void getCustomerPass()
    {
        formatter.outputMessageToConsole("Password :");
    }
    public static String getUserInput()
    {
        Scanner sc=new Scanner(System.in);
        return sc.next();
    }


    public boolean invalidUserOption()
    {
        Scanner sc=new Scanner(System.in);
        formatter.outputMessageToConsole("Invalid Credential .Do you want try again?(y/n)");
        if(sc.next().equalsIgnoreCase("y")) {
            return true;
        }
        return false;
    }

    public int checkUserInput(String input) {

        try{
            int choice= Integer.parseInt(input);
            if(choice>0 && choice<4)
            {
                return choice;
            }
            else if(choice==4)
            {
                return 4;
            }
            throw new Exception();

        }
        catch(Exception e)
        {
            formatter.outputMessageToConsole("Select a valid option!");
            return 0;
        }
    }


    public String menuAction(Library library, int userChoiceInt) {


            switch(userChoiceInt)
            {
                case 1:
                    performBookAction(library);
                    break;
                case 2:
                    performMovieAction(library);
                    break;
                case 3:formatter.outputMessageToConsole(library.getUserDetails());
                    break;

                case 4:
                    return "Thank You ...";

            }
        return "Continue";

     }

    private void performBookAction(Library library)
    {
        boolean flag=true;
        MenuClass menu;

        PrintStream printStream =  System.out;
        formatter = new Formatter(printStream);
        menu=new BookMenu(formatter);
        while(flag)
        {

            menu.showMenu();
            String userChoiceString= menu.getUserInput();
            int userChoiceInt= menu.checkUserInput(userChoiceString);
            if(userChoiceInt!=0 && userChoiceInt!=4)
            {
                formatter.outputMessageToConsole(menu.menuAction(library, userChoiceInt));
            }
            else if(userChoiceInt==4)
            {
                flag=false;
            }
        }
    }
    private void performMovieAction(Library library)
    {
        boolean flag=true;
        MenuClass menu;
        PrintStream printStream =  System.out;
        formatter = new Formatter(printStream);
        menu=new MovieMenu(formatter);
        while(flag)
        {


            menu.showMenu();
            String userChoiceString= menu.getUserInput();
            int userChoiceInt= menu.checkUserInput(userChoiceString);
            if(userChoiceInt!=0 && userChoiceInt!=4)
            {
                formatter.outputMessageToConsole(menu.menuAction(library, userChoiceInt));
            }
            else if(userChoiceInt==4)
            {
                flag=false;
            }
        }
    }




}
